<?php

namespace App\Http\Controllers;


use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
include_once app_path('/Http/Controllers/wxBizDataCrypt.php');

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    // 获取微信用户信息
    public function getWxLogin(Request $request)
    {
        // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php";

        $code = $request->get('code');
        $encryptedData = $request->get('encryptedData');
        $iv = $request->get('iv');
        $appid = "wx4e7f9e9721288267";
        $secret = "fbc2f3da292f617ed828961b4d5903d4";

        $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $apiData = file_get_contents($URL);
        // var_dump($code,'wwwwwwww',$apiData['errscode']);
        //     $ch = curl_init();
        // 　　curl_setopt($ch, CURLOPT_URL, $URL);
        // 　　curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 　　curl_setopt($ch, CURLOPT_HEADER, 0);
        // 　　$output = curl_exec($ch);
        // 　　curl_close($ch)

        if (!isset($apiData['errcode'])) {
            $sessionKey = json_decode($apiData)->session_key;
            $userifo = new \WXBizDataCrypt($appid, $sessionKey);

            $errCode = $userifo->decryptData($encryptedData, $iv, $data);

            if ($errCode == 0) {
                return ($data . "\n");
            } else {
                return false;
            }
        }
    }
    public function test(Request $request)
    {
        //    
    }
}
